$(function () {
    var client_dataGrid = $("#client_dataGrid");
    var client_dialog = $("#client_dialog");
    var car_dataGrid = $("#car_dataGrid");
    var car_dialog = $("#car_dialog");
    var product_dataGrid = $("#product_dataGrid");
    var product_dialog = $("#product_dialog");

    //客户数据表格
    client_dataGrid.datagrid({
        url: "/client/query.do",
        fitColumns: true,//列自适应
        fit: true,
        align: 'center',
        columns: [[
            {
                field: 'clientName', title: '客户姓名', width: 100
            },
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                return value ? "男" : "女";
            }
            },
            {field: 'idCard', title: '身份证', width: 100, align: 'center'},
            {field: 'tel', title: '联系方式', width: 100, align: 'center'},
            {field: 'clientAddress', title: '客户地址', width: 100, align: 'center'}
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        //用户点击一行回显数据
        onClickRow: function () {
            var client = client_dataGrid.datagrid("getSelected");

            $("[name='holder.id']").val(client.id);
            $("#clientName").textbox("setValue", client.clientName);
            $("#gender").combobox("setValue", client.gender);
            $("#idCard").textbox("setValue", client.idCard);
            $("#tel").numberbox("setValue", client.tel);
            $("#clientAddress").textbox("setValue", client.clientAddress);
            /*$("#add_form").form("load", client);*/
            client_dialog.dialog("close");
        }
    });

    //车辆数据表格
    car_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/car/query.do",
        fit: true,
        columns: [[
            {field: 'name', title: '车主姓名', width: 100},
            {
                field: 'carbrand', title: '车辆品牌', width: 100, formatter: function (value) {
                var name;
                $.ajax({
                    type: "POST",
                    url: "/dictionary/selectDictionaryItemById.do",
                    async: false,
                    data: {id: value},
                    success: function (msg) {
                        name = msg.name;
                    }
                });
                return name;
            }
            },
            {field: 'model', title: '车辆型号', width: 100},
            {field: 'number', title: '车牌号码', width: 100},
            {field: 'gas', title: '排气量/L', width: 100},
            {field: 'pdate', title: '购买日期', width: 100},
            {field: 'price', title: '市场估价/万元', width: 100, sortable: true},
            {
                field: 'typestate', title: '分类', width: 100, formatter: function (value, row, index) {
                return value ? "<span style='color: mediumblue'>载客</span>" : "<span style='color: orangered'>载物</span>";
            }
            },
            {
                field: 'weight', title: '车载大小', width: 100, formatter: function (value) {
                var name;
                $.ajax({
                    type: "POST",
                    url: "/dictionary/selectDictionaryItemById.do",
                    async: false,
                    data: {id: value},
                    success: function (msg) {
                        name = msg.name;
                    }
                });
                return name;
            }
            }
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        sortName: 'price',
        sortOrder: 'DESC',
        //用户点击一行回显数据
        onClickRow: function () {
            var car = car_dataGrid.datagrid("getSelected");
            $("#cId").val(car.id);
            $("#cName").textbox("setValue", car.name);
            $("#cBrand").combobox("setValue", car.carbrand);
            $("#cModel").textbox("setValue", car.model);
            $("#cNumber").textbox("setValue", car.number);
            $("#cGas").textbox("setValue", car.gas);
            $("#cDate").datebox("setValue", car.pdate);
            $("#cPrice").numberbox("setValue", car.price);
            $("#cType").combobox("setValue", car.typestate);
            $("#cWeight").combobox("setValue", car.weight);
            /*$("#add_form").form("load", car);*/
            car_dialog.dialog("close");
        }
    });

    //保险信息
    product_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/product/query.do",
        fit: true,
        columns: [[
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {field: 'year', title: '保障年限', width: 100},
            {field: 'money', title: '保额', width: 100},
            {field: 'annualFee', title: '基本年费', width: 100},
            {
                field: 'saleState', title: '销售状态', width: 100, formatter: function (value, row, index) {
                return value ? "在售" : "<font color='red'>停售</font>";
            }
            },
            {
                field: 'deductState', title: '免赔状态', width: 100, formatter: function (value, row, index) {
                return value ? "是" : "<font color='red'>否</font>";
            }
            }
        ]],
        queryParams: {
            saleState: 1,
            companyId: function () {
                return $("#companyId").combobox("getValue");
            }
        },
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        onClickRow: function (index, row) {
            $("#productName").textbox("setValue", row.name);
            $("[name='product.id']").val(row.id);
            $("[tag=sn]").text(row.sn);
            $("#annualFee").textbox("setValue", row.annualFee);
            product_dialog.dialog("close");
        }
    });


    //初始化客户弹出框
    client_dialog.dialog({
        width: 480,
        height: 380,
        closed: true
    });

    //初始化车辆弹出框
    car_dialog.dialog({
        width: 800,
        height: 380,
        closed: true
    });

    //初始化保险信息弹出框
    product_dialog.dialog({
        width: 530,
        height: 380,
        closed: true
    });

    //方法抽成对象统一管理
    var methodObj = {//添加按钮

        //会员客户
        choose_customer: function () {
            client_dialog.dialog("open");
            client_dialog.dialog("setTitle", "选择客户");
        },

        //投保的车辆
        choose_car: function () {
            car_dialog.dialog("open");
            car_dialog.dialog("setTitle", "选择车辆");
        },

        //添加产品
        search: function () {
            product_dialog.dialog("open");
            product_dialog.dialog("setTitle", "选择保险产品");
            product_dataGrid.datagrid("load")
        },

        //取消按钮
        cancel: function () {
            client_dialog.dialog("close")
        },
        subimitTemp: function () {
            $.post("/temOrder/saveOrUpdate.do", $("#addTemp_form").serialize(), function (data) {
                if (data.success) {
                    alert("保存成功");
                } else {
                    alert("保存失败");
                }
            })
        }
    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd](this);
    });
});

function aggregate() {
    var annualFee = $("#annualFee").textbox("getValue");
    var year = $("[name=year]").val();
    var ret = year * annualFee;
    $("[tag=amount]").text(ret.toFixed(2));
}